package com.du.lin.controller;

import com.du.lin.annotation.BizLog;
import com.du.lin.constant.Constant;
import com.du.lin.service.ElectricitySalesManagementService;
import com.du.lin.service.UserService;
import com.du.lin.utils.DateUtil;
import com.du.lin.utils.ExcelUtil;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ElectricitySalesManagement extends BaseController{

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ElectricitySalesManagementService electricitySalesManagementService;

    @Autowired
    private ExcelUtil excelUtil;

    /**
     * 获取表格的列
     * @param param
     * @return
     */
    @PostMapping("/getCloums")
    @ResponseBody
    public Map<String,Object> getCloumName(String param){
        String date =param;

        Map<String,Object> map = new HashMap<>();
        map.put("data","");
        map.put("code",200);
        map.put("success","成功");
        /*if(StringUtils.isEmpty(param.get("date"))){
            map.put("data","");
            map.put("code",20012);
            map.put("success","没有获取到时间");
            return map;
        }
        date = String.valueOf(param.get("date"));*/
        //定义天数的
        int sumDay = 0;
        int month = 0;

        //接受列的名称
        List<String> cloumNmae = new ArrayList<>();
        cloumNmae.add("姓名");
        cloumNmae.add("工作统计");
        cloumNmae.add("总计");
        cloumNmae.add("用户id");
        cloumNmae.add("时间");
        cloumNmae.add("类型标识");
        //接受的列
        List<Object> cloumList = new ArrayList<>();
       /* Map<String,Object> cloumMapId = new HashMap<>();
        cloumMapId.put("name","id");
        cloumMapId.put("index","id");
        cloumMapId.put("editable",true);
        cloumMapId.put("width",90);
        cloumMapId.put("cellattr","");
        cloumList.add(cloumMapId);*/
        Map<String,Object> cloumMap = new HashMap<>();
        cloumMap.put("name","userName");
        cloumMap.put("index","userName");
        //cloumMap.put("editable",true);
        cloumMap.put("width",90);
       /* cloumMap.put("cellattr","rowspan=8");*/
        cloumList.add(cloumMap);
        Map<String,Object> cloumMaptype = new HashMap<>();
        cloumMaptype.put("name","type");
        cloumMaptype.put("index","type");
        //cloumMaptype.put("editable",true);
        cloumMaptype.put("width",130);
        cloumList.add(cloumMaptype);
        Map<String,Object> cloumMapsum = new HashMap<>();
        cloumMapsum.put("name","sum");
        cloumMapsum.put("index","sum");
        //cloumMapsum.put("editable",true);
        cloumMapsum.put("width",90);
        cloumList.add(cloumMapsum);
        //useId
        Map<String,Object> cloumMapUserId = new HashMap<>();
        cloumMapUserId.put("name","userId");
        cloumMapUserId.put("index","userId");
        cloumMapUserId.put("hidden",true);
        cloumMapUserId.put("width",90);
        cloumList.add(cloumMapUserId);
        //date
        Map<String,Object> cloumMapDate = new HashMap<>();
        cloumMapDate.put("name","date");
        cloumMapDate.put("index","date");
        cloumMapDate.put("hidden",true);
        cloumMapDate.put("width",90);
        cloumList.add(cloumMapDate);
        //isType
        Map<String,Object> cloumMapIsType = new HashMap<>();
        cloumMapIsType.put("name","isType");
        cloumMapIsType.put("index","isType");
        cloumMapIsType.put("hidden",true);
        cloumMapIsType.put("width",90);
        cloumList.add(cloumMapIsType);


        if(StringUtils.isEmpty(date)){
            map.put("data","");
            map.put("code",2001);
            map.put("success","时间不能为空");
            return map;
        }
        //获取当前月的天数
        try {
            sumDay = DateUtil.getDaysByYearMonth(date);
            month= DateUtil.getDaysByMonth(date);
        }catch (Exception e){
            map.put("data","");
            map.put("code",2002);
            map.put("success","获取月的天数失败");
            return map;
        }
        for(int i=0;i<sumDay;i++){
            int j = i+1;
            cloumNmae.add(j+"");
            Map<String,Object> cloumMaptypes = new HashMap<>();
            cloumMaptypes.put("name","day"+j);
            cloumMaptypes.put("index","day"+j);
            cloumMaptypes.put("editable",true);
            cloumMaptypes.put("width",90);
            cloumList.add(cloumMaptypes);
        }
        cloumNmae.add("操作");
        Map<String,Object> cloumMaphandle = new HashMap<>();
        cloumMaphandle.put("name","handle");
        cloumMaphandle.put("index","handle");
        cloumMaphandle.put("sortable",false);
        cloumMaphandle.put("width",100);
        cloumList.add(cloumMaphandle);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("sumDay",sumDay);
        resultMap.put("month",month);
        resultMap.put("cloumList",cloumList);
        resultMap.put("cloumNmae",cloumNmae.toArray());
        map.put("data",resultMap);
        return map;
    }



    @ResponseBody
    @RequestMapping(value="/salesManagementlist" , method={RequestMethod.POST})
    public String list(HttpServletRequest request){
        String page = request.getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数
        String rows = request.getParameter("rows"); //
        String dept = request.getParameter("dept");
        String starttime = request.getParameter("starttime");
        return electricitySalesManagementService.getAllUserJson(Integer.parseInt(page) , Integer.parseInt(rows),dept,starttime);
    }


    @ResponseBody
    @RequestMapping(value ="/addSalesManagerment" , method={RequestMethod.POST})
    public String addSalesManagerment(HttpServletRequest request){
        String dataparam = request.getParameter("dataparam");
        String userId = request.getParameter("userId");
        String date = request.getParameter("date");
        String isType = request.getParameter("isType");
        return  electricitySalesManagementService.addSalesManagerment(dataparam,userId,date,isType);
    }

    @BizLog("删除用户")
    @ResponseBody
    @RequestMapping(value="/deletesales" , method=RequestMethod.POST)
    public String deletesales(HttpServletRequest request){
        String userId = request.getParameter("userId");
        String date = request.getParameter("date");
        return electricitySalesManagementService.deletesales(userId,date)+"";
    }

    @BizLog("下载exel")
    @RequestMapping(value="/downsalesgexcel" , method={RequestMethod.GET} )
    public String downsalesgexcel(HttpServletRequest request , HttpServletResponse response, @RequestParam("dept") String dept,@RequestParam("starttime") String starttime){
        List<Object> list = electricitySalesManagementService.getList(dept,starttime);
        downloadFile(response, excelUtil.getSalesExcel(list));
            return null;
    }
}
